package g;

import com.pgl.sys.ces.out.ISdkLite;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.Objects;

/* compiled from: RealBufferedSource */
public final class u implements g {
    public final e a = new e();
    public final z b;
    public boolean c;

    /* compiled from: RealBufferedSource */
    public class a extends InputStream {
        public int available() throws IOException {
            u uVar = u.this;
            if (!uVar.c) {
                return (int) Math.min(uVar.a.b, 2147483647L);
            }
            throw new IOException("closed");
        }

        public void close() throws IOException {
            u.this.close();
        }

        public int read() throws IOException {
            u uVar = u.this;
            if (uVar.c) {
                throw new IOException("closed");
            }
            e eVar = uVar.a;
            if (eVar.b == 0 && uVar.b.c(eVar, 8192) == -1) {
                return -1;
            }
            return u.this.a.q0() & ISdkLite.REGION_UNSET;
        }

        public String toString() {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(u.this);
            stringBuilder.append(".inputStream()");
            return stringBuilder.toString();
        }

        public int read(byte[] bArr, int i, int i2) throws IOException {
            if (u.this.c) {
                throw new IOException("closed");
            }
            b0.b((long) bArr.length, (long) i, (long) i2);
            u uVar = u.this;
            e eVar = uVar.a;
            if (eVar.b == 0 && uVar.b.c(eVar, 8192) == -1) {
                return -1;
            }
            return u.this.a.u(bArr, i, i2);
        }
    }

    public u(z zVar) {
        Objects.requireNonNull(zVar, "source == null");
        this.b = zVar;
    }

    public e A() {
        return this.a;
    }

    public a0 B() {
        return this.b.B();
    }

    public int J() throws IOException {
        i0(4);
        return this.a.J();
    }

    public String M() throws IOException {
        return b0(Long.MAX_VALUE);
    }

    public int N() throws IOException {
        i0(4);
        return this.a.N();
    }

    public boolean Q() throws IOException {
        if (!this.c) {
            return this.a.Q() && this.b.c(this.a, 8192) == -1;
        } else {
            throw new IllegalStateException("closed");
        }
    }

    public byte[] S(long j) throws IOException {
        if (i(j)) {
            return this.a.S(j);
        }
        throw new EOFException();
    }

    public short X() throws IOException {
        i0(2);
        return this.a.X();
    }

    public long Y(h hVar) throws IOException {
        if (this.c) {
            throw new IllegalStateException("closed");
        }
        long j = 0;
        while (true) {
            long t = this.a.t(hVar, j);
            if (t != -1) {
                return t;
            }
            e eVar = this.a;
            long j2 = eVar.b;
            if (this.b.c(eVar, 8192) == -1) {
                return -1;
            }
            j = Math.max(j, j2);
        }
    }

    public long a(byte b, long j, long j2) throws IOException {
        if (this.c) {
            throw new IllegalStateException("closed");
        } else if (j < 0 || j2 < j) {
            throw new IllegalArgumentException(String.format("fromIndex=%s toIndex=%s", new Object[]{Long.valueOf(j), Long.valueOf(j2)}));
        } else {
            while (j < j2) {
                long s = this.a.s(b, j, j2);
                if (s == -1) {
                    e eVar = this.a;
                    long j3 = eVar.b;
                    if (j3 >= j2 || this.b.c(eVar, 8192) == -1) {
                        break;
                    }
                    j = Math.max(j, j3);
                } else {
                    return s;
                }
            }
            return -1;
        }
    }

    public String b0(long j) throws IOException {
        if (j >= 0) {
            long j2 = j == Long.MAX_VALUE ? Long.MAX_VALUE : j + 1;
            long a = a((byte) 10, 0, j2);
            if (a != -1) {
                return this.a.u0(a);
            }
            if (j2 < Long.MAX_VALUE && i(j2) && this.a.r(j2 - 1) == (byte) 13 && i(1 + j2) && this.a.r(j2) == (byte) 10) {
                return this.a.u0(j2);
            }
            e eVar = new e();
            e eVar2 = this.a;
            eVar2.o(eVar, 0, Math.min(32, eVar2.b));
            StringBuilder g = c.b.a.a.a.g("\\n not found: limit=");
            g.append(Math.min(this.a.b, j));
            g.append(" content=");
            g.append(eVar.v().k());
            g.append(8230);
            throw new EOFException(g.toString());
        }
        throw new IllegalArgumentException(c.b.a.a.a.B("limit < 0: ", j));
    }

    public long c(e eVar, long j) throws IOException {
        if (eVar == null) {
            throw new IllegalArgumentException("sink == null");
        } else if (j < 0) {
            throw new IllegalArgumentException(c.b.a.a.a.B("byteCount < 0: ", j));
        } else if (this.c) {
            throw new IllegalStateException("closed");
        } else {
            e eVar2 = this.a;
            if (eVar2.b == 0 && this.b.c(eVar2, 8192) == -1) {
                return -1;
            }
            return this.a.c(eVar, Math.min(j, this.a.b));
        }
    }

    public short c0() throws IOException {
        i0(2);
        return this.a.c0();
    }

    public void close() throws IOException {
        if (!this.c) {
            this.c = true;
            this.b.close();
            this.a.n();
        }
    }

    public h d(long j) throws IOException {
        if (i(j)) {
            return this.a.d(j);
        }
        throw new EOFException();
    }

    public void f(long j) throws IOException {
        if (this.c) {
            throw new IllegalStateException("closed");
        }
        while (j > 0) {
            e eVar = this.a;
            if (eVar.b == 0 && this.b.c(eVar, 8192) == -1) {
                throw new EOFException();
            }
            long min = Math.min(j, this.a.b);
            this.a.f(min);
            j -= min;
        }
    }

    public boolean i(long j) throws IOException {
        if (j < 0) {
            throw new IllegalArgumentException(c.b.a.a.a.B("byteCount < 0: ", j));
        } else if (this.c) {
            throw new IllegalStateException("closed");
        } else {
            e eVar;
            do {
                eVar = this.a;
                if (eVar.b >= j) {
                    return true;
                }
            } while (this.b.c(eVar, 8192) != -1);
            return false;
        }
    }

    public void i0(long j) throws IOException {
        if (!i(j)) {
            throw new EOFException();
        }
    }

    public boolean isOpen() {
        return this.c ^ 1;
    }

    public void k(byte[] bArr) throws IOException {
        try {
            i0((long) bArr.length);
            this.a.w(bArr);
        } catch (EOFException e) {
            int i = 0;
            while (true) {
                e eVar = this.a;
                long j = eVar.b;
                if (j > 0) {
                    int u = eVar.u(bArr, i, (int) j);
                    if (u != -1) {
                        i += u;
                    } else {
                        throw new AssertionError();
                    }
                }
                throw e;
            }
        }
    }

    public long l0(byte b) throws IOException {
        return a(b, 0, Long.MAX_VALUE);
    }

    public boolean m0(long j, h hVar) throws IOException {
        int p = hVar.p();
        if (this.c) {
            throw new IllegalStateException("closed");
        } else if (j < 0 || p < 0 || hVar.p() - 0 < p) {
            return false;
        } else {
            int i = 0;
            while (i < p) {
                long j2 = ((long) i) + j;
                if (!i(1 + j2) || this.a.r(j2) != hVar.j(0 + i)) {
                    return false;
                }
                i++;
            }
            return true;
        }
    }

    public long n0() throws IOException {
        i0(1);
        int i = 0;
        while (true) {
            int i2 = i + 1;
            if (!i((long) i2)) {
                break;
            }
            byte r = this.a.r((long) i);
            if ((r >= (byte) 48 && r <= (byte) 57) || ((r >= (byte) 97 && r <= (byte) 102) || (r >= (byte) 65 && r <= (byte) 70))) {
                i = i2;
            } else if (i == 0) {
                throw new NumberFormatException(String.format("Expected leading [0-9a-fA-F] character but was %#x", new Object[]{Byte.valueOf(r)}));
            }
        }
        return this.a.n0();
    }

    public String o0(Charset charset) throws IOException {
        if (charset != null) {
            this.a.G(this.b);
            e eVar = this.a;
            Objects.requireNonNull(eVar);
            try {
                return eVar.h0(eVar.b, charset);
            } catch (EOFException e) {
                throw new AssertionError(e);
            }
        }
        throw new IllegalArgumentException("charset == null");
    }

    public InputStream p0() {
        return new a();
    }

    public byte q0() throws IOException {
        i0(1);
        return this.a.q0();
    }

    public int r0(s sVar) throws IOException {
        if (this.c) {
            throw new IllegalStateException("closed");
        }
        do {
            int v0 = this.a.v0(sVar, true);
            if (v0 == -1) {
                return -1;
            }
            if (v0 != -2) {
                this.a.f((long) sVar.a[v0].p());
                return v0;
            }
        } while (this.b.c(this.a, 8192) != -1);
        return -1;
    }

    public int read(ByteBuffer byteBuffer) throws IOException {
        e eVar = this.a;
        if (eVar.b == 0 && this.b.c(eVar, 8192) == -1) {
            return -1;
        }
        return this.a.read(byteBuffer);
    }

    public String toString() {
        StringBuilder g = c.b.a.a.a.g("buffer(");
        g.append(this.b);
        g.append(")");
        return g.toString();
    }
}
